#!/bin/bash

#默认配置
ttnode_node="edsnode"
cache_dir="/mnts"
VDIS="arm64"
OSIS="linux"
PACKAGE="unzip"

#判断CPU架构
sysArch(){
	ARCH=$(uname -m)
	if   [[ "$ARCH" == "i686" ]] || [[ "$ARCH" == "i386" ]]; then
		VDIS="32"
	elif [[ "$ARCH" == "x86_64" ]] ; then
		VDIS="amd64"
	elif [[ "$ARCH" == *"armv7"* ]] || [[ "$ARCH" == "armv6l" ]]; then
		VDIS="arm"
	elif [[ "$ARCH" == *"armv8"* ]] || [[ "$ARCH" == "aarch64" ]]; then
		VDIS="arm64"
	elif [[ "$ARCH" == *"mips64le"* ]]; then
		VDIS="mips64le"
	elif [[ "$ARCH" == *"mips64"* ]]; then
		VDIS="mips64"
	elif [[ "$ARCH" == *"mipsle"* ]]; then
		VDIS="mipsle"
	elif [[ "$ARCH" == *"mips"* ]]; then
		VDIS="mips"
	elif [[ "$ARCH" == *"s390x"* ]]; then
		VDIS="s390x"
	elif [[ "$ARCH" == "ppc64le" ]]; then
		VDIS="ppc64le"
	elif [[ "$ARCH" == "ppc64" ]]; then
		VDIS="ppc64"
	fi
	return 0
}

#判断系统版本
sysOS(){
	OS_Release=$(cat /etc/os-release | grep -w ID | sed 's/\"//g' | cut -d '=' -f 2) &> /dev/null
	if [[ -z "$OS_Release" ]]; then
    		echo "无法识别当前系统，将继续执行，可能存在不兼容等问题，可按ctrl+c中断退出"
		sleep 5s
	else	
		echo "当前系统是$VDIS版$OS_Release"
		OSIS=$OS_Release
		sleep 5s
	fi
}

#选择节点
choose_node() {
    echo
    echo "=============请选择要运行的节点(默认为edsnode)============="
    echo "| 1 - edsnode"
    echo "| 2 - kanode"
    echo "| 3 - lsnode"
    echo "| 4 - wsnode"
    echo "| q - 退出"
    echo "---------------------------------"
    printf "请选择节点（输入数字1-4或q）："
    read -n 1 n
    echo
    if [[ "$n" = "1" || "$n" = "" ]]; then
        ttnode_node="edsnode"
    elif [ "$n" = "2" ]; then
        ttnode_node="kanode"
    elif [ "$n" = "3" ]; then
        ttnode_node="lsnode"
    elif [ "$n" = "4" ]; then
        ttnode_node="wsnode"
    elif [ "$n" = "q" ]; then
        echo 退出
        sleep 2s
        exit 0
    else
        echo "输入错误！请重新选择！"
        sleep 3s
        choose_node
        return
    fi
    echo "要运行的节点为$ttnode_node"
    sleep 3s
}

#设置缓存路径
set_cache() {
	read -p "请输入缓存路径（默认路径/mnts/ttnode）:" cache_input
if [[ -d "$cache_input" || ! "$cache_input" ]]; then
	echo "使用默认路径或输入的路径"
	if [ ! -n "$cache_input" ]; then
		echo "使用默认缓存路径/mnts"
	else
		echo "你设置的缓存路径为$cache_input/ttnode"
		cache_dir=$cache_input
	fi	
else
	echo "缓存路径不存在，请检查路径是否正确/存在，若不存在请自行创建，5s后将提示再次输入，按ctrl+c退出命令"
	sleep 5s
	set_cache
	return 
fi

if [ "$cache_dir" = "/mnts" ]; then
	echo "使用默认路径"
	check_defual_cache
else
	echo "使用用户设置路径"
	if [ -d "$cache_dir/ttnode" ]; then
		echo "$cache_dir/ttnode存在"
	else
		echo "$cache_dir/ttnode不存在，将自动创建"
		mkdir $cache_dir/ttnode
	fi
fi
sleep 3s
}

#检查默认路径是否存在
check_defual_cache() {
if [ -d "/mnts" ]; then
	echo "/mnts存在,注意硬盘的挂载目录是否为/mnts"
	if [ -d "/mnts/ttnode" ]; then
		echo "/mnts/ttnode存在"
	else
		echo "/mnts/ttnode不存在，将自动创建目录"
		mkdir /mnts/ttnode
	fi
else
	echo "/mnts不存在，将自动创建目录，请将缓存硬盘挂载到/mnts目录并在/mnts目录下手动创建ttnode目录！！！"
	mkdir /mnts
	mkdir /mnts/ttnode
fi
}

#开始部署
start_run() {

#检查是否安装解压程序
if [[ "$OSIS" == "debian" ]] || [[ "$OSIS" == "ubuntu" ]]; then
	dpkg -s $PACKAGE &> /dev/null
	if [ $? -eq 0 ]; then
   		echo "$PACKAGE is installed"
	else
    		apt update
    		apt install -y zip unzip
	fi
fi

rm -rf /usr/node
rm -rf ./ttmanager.zip
rm -rf ./ttmanager

#下载甜糖程序
echo '正在下载甜糖程序，请稍等'
wget https://gitee.com/zhang0510/share/releases/download/$VDIS/ttmanager.zip
unzip ./ttmanager.zip

mkdir /usr/node 
mv ./ttmanager /usr/node/
chmod -R 777 /usr/node/

#开始运行甜糖程序
/usr/node/ttmanager -g
#启动
nohup /usr/node/run.sh -t $ttnode_node -c $cache_dir/ttnode > /dev/null 2>&1 & 

#加入开机自启
sed -i '/run.sh/d' /etc/rc.local
sed -i "/exit 0/i\nohup /usr/node/run.sh -c $cache_dir/ttnode > /dev/null 2>&1 & " /etc/rc.local
}


sysArch
echo "当前的CPU架构是$VDIS"
if [[ "$VDIS" == "amd64" ]] || [[ "$VDIS" == "arm64" ]] || [[ "$VDIS" == "arm" ]]; then
	echo "符合要求，将继续执行"
	sleep 2s;
else
	echo "检查你的CPU架构是否是arm/arm64/x86_64！！即将退出！！"
	sleep 3s;
	exit 0
fi

sysOS
choose_node
set_cache
start_run